var listVue = new Vue({
	el : '#salesBackOfYear',
	data : {
		projectLists : [],
		params : {
			projectId : null,
			year : '',
			saleReturnFlag : ''
		},
		salesBackOfYear : {
			actualSalesAmount : 0,
			actualSalesBack : 0,
			targetSalesAmount : 0,
			targetSalesBack : 0
		},
		sales : [],
		salesplan : [],
		backs : [],
		backsplan : [],
		isShow : false
	},
	mounted : function() {

		this.projectList();
	},
	methods : {
		projectList : function() {
			var _self = this;
			var url = contextPath + "/estate/project/findProjectList";
			var params = {
				resourcesCode : 'menu_0003_0001',
				resourcesName : 'resName_pro_list'
			};

			$.ajax({
				url : url,
				data : params,
				dataType : 'json',
				success : function(res) {
					_self.projectLists = res.root;
				}
			});
		},
		findSales : function() {
			var _self = this;
			if (!_self.params.projectId) {
				layer.alert("请选择项目!");
			} else if (!_self.params.year) {
				layer.alert("请选择年份");
			} else {
				this.returnDataHighcharts();
				this.saleDataHighcharts();
			}
		},
		returnDataHighcharts : function() {
			var _self = this;
			var url = contextPath + "/wordFlowData/findProjectPlan";
			_self.params.saleReturnFlag = 3;
			$.ajax({
				url : url,
				data : _self.params,
				dataType : 'json',
				success : function(res) {
					if (res != null && res[1] != undefined) {
						_self.backs = res[1].data;
						_self.backsplan = res[0].data;
						_self.salesBackOfYear.actualSalesBack = _self.sumArray(
								res[1].data).toFixed(6);
						_self.salesBackOfYear.targetSalesAmount = _self
								.sumArray(res[0].data).toFixed(6);
						_self.createBrokenLine('returnDataHighcharts', '回款计划',
								res);
					}
				}
			});
		},
		saleDataHighcharts : function() {
			var index = layer.load();
			var _self = this;
			var url = contextPath + "/wordFlowData/findProjectPlan";
			_self.params.saleReturnFlag = 1
			$.ajax({
				url : url,
				data : _self.params,
				dataType : 'json',
				success : function(res) {

					if (res != null && res[1] != undefined) {
						_self.sales = res[1].data;
						_self.salesplan = res[0].data;
						_self.salesBackOfYear.actualSalesAmount = _self
								.sumArray(res[1].data).toFixed(6);
						_self.salesBackOfYear.targetSalesBack = _self.sumArray(
								res[0].data).toFixed(6);
						_self.createBrokenLine('salesDataHighcharts', '销售计划',
								res);
						_self.isShow = true;
					} else {
						layer.alert("项目在" + _self.params.year + "年,不存在数据!");
						_self.isShow = false;
					}
					layer.close(index);
				}
			});

		},
		createBrokenLine : function(divId, title, data) {
			var _self = this;
			Highcharts.chart({
				chart : {
					type : 'line',
					renderTo : divId,
					plotBackgroundColor : null,
					plotBorderWidth : null,
					plotShadow : false

				},
				title : {
					text : title
				},
				subtitle : {
					text : '数据来源: M2绿城管理系统'
				},
				xAxis : {
					categories : [ '一月', '二月', '三月', '四月', '五月', '六月', '七月',
							'八月', '九月', '十月', '十一月', '十二月' ]
				},
				yAxis : {
					title : {
						text : '单位:万元'
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						// 开启数据标签
						},
						enableMouseTracking : true
					// 关闭鼠标跟踪，对应的提示框、点击事件会失效
					}
				},
				series : data
			});
		},
		sendLevelDynamiSalesData : function() {
			var _self = this;
			_self.salesBackOfYear.year = _self.params.year;
			var url = contextPath
					+ "/pmo/salesBackOfYearPmo/sendLevelProjectData?projectId="
					+ _self.params.projectId;
			$.ajax({
				url : url,
				data : _self.salesBackOfYear,
				dataType : 'json',
				success : function(data) {
					if (data.res == 1) {
						layer.alert("推送成功!接口参数请去系统日志中查看!");
					} else {
						layer.alert("推送失败,请检查接口参数!");
					}
				}
			});

		},
		sumArray : function(arr) {
			var _self = this;
			var len = arr.length;
			if (len == 0) {
				return 0;
			} else if (len == 1) {
				return arr[0];
			} else {
				return arr[0] + _self.sumArray(arr.slice(1));
			}
		},
		sendStaticSalesData : function() {
			var _self = this;
			var params = {
				projectId : _self.params.projectId
			};
			var url = contextPath + "/estate/project/sendStaticSalePmo";
			$.ajax({
				url : url,
				data : params,
				dataType : 'json',
				success : function(data) {
					if (data.success) {
						layer.alert("推送成功!接口参数请去系统日志中查看!");
					} else {
						layer.alert("推送失败,请检查接口参数!");
					}
				}
			});

		}
	}
});